<?php
namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\Lib\Response;
use App\Http\Model\Hint;
use App\Http\Services\CounterServices;
use App\Http\Services\UserServices;
use App\Http\Services\SendGiftServices;
use App\Http\Model\SystemConfig;

class ApiCounterController extends BaseController
{
    protected $servicesCounter;
    protected $userServices;
    protected $sendGiftServices;
    private $systemConfigModel;
    public function __construct()
    {
        $this->servicesCounter = new CounterServices();
        $this->userServices = new UserServices();
        $this->sendGiftServices = new SendGiftServices();
        $this->systemConfigModel = new SystemConfig();
    }
    /**
     *  赠送自己礼物开关是否开启
     * */

    public function giveSelfGift()
    {

        $config=$this->systemConfigModel->getDefaultSystemConfig();
        $data['send_self_gift_show'] = $config['send_self_gift_show']['value'];
        Response::returnResult(Hint::SUCCESS, $data);

    }
    /**
     *  货架标签/分类列表接口
     * */
    public function cateList(Request $request)
    {
        $id = $request->get('id') ?? 0; //1=cbd 2=小窝 3=幸运 5=私聊
        $roomId = $request->get('room_id', 0);
        $userId = $this->getLoginUserId();

        $data = $this->servicesCounter->getCounterCate($id, $roomId, $userId);

        Response::returnResult(Hint::SUCCESS, $data);
    }

    /**
     *  列表接口
     * */
    public function list(Request $request)
    {
        $userId = $this->getLoginUserId();
        if(empty($userId)) {
            Response::returnResult(Hint::LOGIN_OUT);
        }

        $id = $request->get('id') ?? 1; // 默认值 1
        $roomId = $request->get('room_id');
        $cateId = $request->get('cate_id') ?? 0;

        $data = $this->servicesCounter->getCounter($roomId, $id, $cateId, $userId);

        Response::returnResult(Hint::SUCCESS, $data);
    }

    /**
     *  我的货架列表
     * */
    public function myList(){
        $userId = $this->getLoginUserId();
        if(empty($userId)) {
            Response::returnResult(Hint::LOGIN_OUT);
        }
        $data = $this->servicesCounter->getMyCounter($userId);

        Response::returnResult(Hint::SUCCESS, $data);
    }

    //我的货架赠送
    public function give(Request $request){
        $goodsType   = $request->post('goods_type');
        $goodsId     = $request->post('goods_id');
        $toUserIdArr = $request->post('to_user_id_arr');
        $roomId      = $request->post('room_id') ?? 0;
        $goodsNum    = $request->post('goods_num');
        $abnormal    = $request->post('abnormal');
        $toUserIdArr = array_unique(array_filter(explode('#', $toUserIdArr)));

        if (empty($goodsType) || empty($goodsId) || empty($toUserIdArr) || empty($goodsNum)){
            Response::returnResult(Hint::PARAM_ERROR);
        }
        //同实名之间不能送礼物
        if ($this->userServices->isSameIdCardNo($toUserIdArr)) {
            //Response::returnResult(Hint::PARAM_ERROR, [], '同实名账号之间不允许打赏哦~');
        }
        if($goodsType != 1){
            Response::returnResult(Hint::PARAM_ERROR, [], '建设中');
        }

        //背包赠送礼物
        $result = $this->sendGiftServices->sendGiftByBag($this->getLoginUserId(), $toUserIdArr, $goodsId, $goodsNum, $roomId, $abnormal);

        Response::returnResult($result['code'], $result['data'], $result['message']);
    }

    /**
     *  购买&赠送
     * */
    public function buy(Request $request){
        $goodsType   = $request->post('goods_type');
        $goodsId     = $request->post('goods_id');
        $toUserIdArr = $request->post('to_user_id_arr', '');
        $roomId      = $request->post('room_id') ?? 0;
        $goodsNum  = $request->post('goods_num');
        $abnormal    = $request->post('abnormal');
        $is_anonym    = $request->post('is_anonym') ?? 0;
        $group_id = $request->post('group_id') ?? 0;
        $counterId = $request->post('counter_id', 0);
        $toUserIdArr = array_unique(array_filter(explode('#', $toUserIdArr)));

        if (empty($goodsType) || empty($goodsId) || empty($toUserIdArr) || empty($goodsNum)){
            Response::returnResult(Hint::PARAM_ERROR, [], '参数缺失 错误编码:112');
        }

        if($goodsType == 1) {
            $data['login_user_id'] = $this->getLoginUserId();
            $data['goods_type'] = $goodsType;
            $data['gift_id'] = $goodsId;
            $data['to_user_ids'] = $toUserIdArr;
            $data['room_id'] = $roomId;
            $data['gift_num'] = $goodsNum;
            $data['abnormal'] = $abnormal;
            $data['is_anonym'] = $is_anonym;
            $data['group_id'] = $group_id;
            $data['counter_id'] = $counterId;

            //获取礼物类型
            $giftInfo = $this->sendGiftServices->getGiftTypeByGiftId($goodsId);
            $giftType = $giftInfo['gift_type'] ?? -1;
            //大部分礼物
            if(!empty($giftType) && in_array($giftType, [1, 2, 3, 4, 6, 8])) {
                //购买赠送礼物
                $result = $this->sendGiftServices->sendGiftByDiamond($this->getLoginUserId(), $toUserIdArr, $goodsId, $goodsNum, $roomId, $abnormal);
                Response::returnResult($result['code'], $result['data'], $result['message']);
            }else if(in_array($giftType, [5])){
                //彩蛋礼物
                while (!redis_lock("color-gift-room-" . $roomId.'-'.$this->getLoginUserId(),3)) {
                    Response::returnResult(Hint::ERR_NORMAL, [], '送礼处理中,请稍后');
                }
                //彩蛋送礼物
                $result = $this->sendGiftServices->sendGiftByDiamondByColor($this->getLoginUserId(), $toUserIdArr, $goodsId, $goodsNum, $roomId, $abnormal);
                Response::returnResult($result['code'], $result['data'], $result['message']);
            }
            else if ($giftType == 7) {
                $user_id = $this->getLoginUserId();
//                if (!in_array($user_id, [10005])) {
//                    Response::returnResult(Hint::PARAM_ERROR, [], '暂无权限');
//                }
                //新幸运礼物连击
                $result = $this->sendGiftServices->newLuckGiftCombo($user_id, $toUserIdArr, $goodsId, $goodsNum, $roomId, $giftInfo);
                Response::returnResult($result['code'], $result['data'], $result['message']);
            }
        } else {
            Response::returnResult(Hint::PARAM_ERROR, [], '建设中');
        }
       
    }

    //背包礼物批量赠送
    public function bagGiftSendBatch(Request $request){
        $toUserId = $request->input('to_user_id');
        $roomId = $request->input('room_id');
        if (empty($toUserId)){
            Response::returnResult(Hint::PARAM_ERROR);
        }

        //背包礼物批量赠送
        $result = $this->sendGiftServices->bagGiftSendBatch($this->getLoginUserId(), $toUserId, $roomId);

        Response::returnResult($result['code'], $result['data'], $result['message']);
    }

    //表白墙送礼物接口 type=1广场入口 type=2或者其他是礼物记录

    public function confessionWall(Request $request){

        $type= $request->input('type');
        if($type==1){
            $limit=20;
        }else{
            $limit=50;
        }
        $result = $this->sendGiftServices->getStreamList($limit);
        Response::returnResult($result['code'], $result['data'], $result['message']);
    }


}
